#include <Arduino.h>
#include <Wire.h>
#include <My_BMP280.h>
#include <OneButton.h>
#include <WiFi.h>
#include "FreeRTOSConfig.h"
//#include "demo.h"

#include "BandGUI.h"
#include "config.h"
#include "Senior.h"
#include "Control.h"

byte pagenum = 0;   //第n个界面
/*
 * 0 主界面
 * 1 心率+血氧界面
 * 2 气压计
 * 3 灯光设置界面
 * 4 设置界面
 */
bool begin_Longpress_State = false;
bool long_press_state = false;
bool light_state = false;
bool gettemp = false;

Senior senior;
Control control;
BMP280 bmp;
BandGUI GUI;
OneButton button(BUTTON_PIN, false);

void click();
void doubleclick();
void duringLongPress();
void longPressStop();
void longPressStart(){
    begin_Longpress_State = true;
};

// 任务一
void TaskButtonTick(void *pvParameters)
{
    (void) pvParameters;
    while(true)
    {
        button.tick();
        if(long_press_state == true && pagenum==0){
            digitalWrite(EN_PIN,LOW);
        }
        vTaskDelay(10);
    }
}

// 任务二
void TaskTempRead(void *pvParameters)
{
    (void) pvParameters;
    while(true)
    {   if(gettemp== true){
            senior.requestTemperatures();
        }
        vTaskDelay(1000);
    }
}

void configWiFi(){
    WiFi.mode(WIFI_STA);
    WiFi.begin("Tenda_178C90","mi989898");
    int tempnum = 0;
    control.setRGB(0,0,10,10);
    while(WiFi.status() != WL_CONNECTED && tempnum<=10){
        delay(500);
        Serial.print(".");
        control.setRGB(0,0,10,10*tempnum);
    }
    if(tempnum>10){
        control.setRGB(1,0,0,0);
        control.setRGB(0,20,30,5);
    }else{
        control.setRGB(0,0,0,0);
        control.setRGB(1,20,30,5);
        Serial.print("IP Address: ");
        Serial.println(WiFi.localIP());
        Serial.println("Connected");
        Serial.print("MAC Address: ");
        Serial.println(WiFi.macAddress()); //MAC地址
        control.updateTime();
    }
    WiFi.channel();
}

void setup() {
    Serial.begin(115200);
    /*
     * 按键初始化
     */
    button.reset();//清除一下按钮状态机的状态
    button.attachClick(click);//注册单击
    button.attachDoubleClick(doubleclick);//注册双击
    button.attachLongPressStart(longPressStart);//注册长按开始
    button.attachDuringLongPress(duringLongPress);//注册长按
    button.attachLongPressStop(longPressStop);//注册长按结束
    button.setLongPressIntervalMs(5000);

    //初始化
    Wire.begin(SDA_PIN, SCL_PIN);
    bmp.begin();
    senior.init();
    control.init();
    control.setBrightness(5);
    control.setRGB(0,20,0,50);
    control.Audio_play("start");
//    control.Audio_play("poweroff");
//    control.Audio_play("music1");
//    control.Audio_play("music2");
    GUI.init();

    ledcWrite(BEE_PWM_CHANNEL,0);
    ledcWrite(LED_PWM_CHANNEL,1);
    delay(2000);
//    GUI.BPM_Init();
//    GUI.MainWin();
//    show_demo();

    xTaskCreate(
            TaskButtonTick
            ,  "Task Button" // A name just for humans
            ,  2048        // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
            ,  NULL // Task parameter which can modify the task behavior. This must be passed as pointer to void.
            ,  1  // Priority
            ,  NULL // Task handle is not used here - simply pass NULL
    );
    xTaskCreate(
            TaskTempRead
            ,  "Task Temp" // A name just for humans
            ,  1024        // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
            ,  NULL // Task parameter which can modify the task behavior. This must be passed as pointer to void.
            ,  2  // Priority
            ,  NULL
            );
}


void demo(){
    //    senior.update();
//    senior.requestTemperatures();
//    control.getTime();
//
//    Serial.print("AHT10 Temperature = ");
//    Serial.print(senior.getTemperature());
//    Serial.println(" *C");
//
//    Serial.print("AHT10 Humidity = ");
//    Serial.print(senior.getHumidity());
//    Serial.println("%RH");
//
//    Serial.print("SKin Temperature = ");
//    Serial.print(senior.getSkinTemperature());
//    Serial.println(" *C");
//
//    Serial.print("Temperature = ");
//    Serial.print(bmp.readTemperature());
//    Serial.println(" *C");
//
//    Serial.print("Pressure = ");
//    Serial.print(bmp.readPressure());
//    Serial.println(" Pa");
//
//    Serial.print("Approx altitude = ");
//    Serial.print(bmp.readAltitude()); // this should be adjusted to your local forcase
//    Serial.println(" m");
// Print RTC
//    Serial.printf("%04d/%02d/%02d %02d:%02d:%02d\n",
//                  dateStruct.year,
//                  dateStruct.month,
//                  dateStruct.date,
//                  timeStruct.hours,
//                  timeStruct.minutes,
//                  timeStruct.seconds
//    );
//    GUI.MainWin();
//    Serial.println();
//    GUI.BPM_With_SPO2();
//    GUI.MPressure();
}

void loop()
{
    switch (pagenum) {
        case 0:
            senior.update();
            gettemp = true;
            control.getTime();
            GUI.MainWin();
            delay(500);
            break;
        case 1:
            gettemp = false;
            GUI.BPM_Init();
            pagenum += 1;
            break;
        case 2:
            GUI.BPM_With_SPO2();
            delay(500);
            break;
        case 3:
            GUI.BPM_ShutDown();
            pagenum += 1;
            break;
        case 4:
            GUI.MPressure();
            delay(1000);
            break;
        case 5:
            configWiFi();
            pagenum = 0;
        default:
            break;
    }
}

void click()
{
    Serial.println("click");
    control.Audio_play("music1");
    pagenum = pagenum<5?pagenum+1:0;
}

void doubleclick(){
    Serial.println("doubleclick");
    light_state =! light_state;
    if(light_state== true){
        ledcWrite(LED_PWM_CHANNEL,50);
    }else{
        ledcWrite(LED_PWM_CHANNEL,0);
    }
    delay(1000);
}

void duringLongPress(){
    begin_Longpress_State =! begin_Longpress_State;
    if(begin_Longpress_State == true &&button.isLongPressed()){
        long_press_state = true;
        control.Audio_play("poweroff");
        Serial.println("Closing");
    }
};

void longPressStop()
{
    Serial.println("longPressStop");
    long_press_state = false;
}